package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.CarWashRecord;
import com.wcar.common.entity.FrontUser;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.StantionDao;
import com.wcar.store.dao.WashRecordDao;
import com.wcar.store.service.WashRecordService;
import com.wcar.store.vo.BindPlusVo;
import com.wcar.store.vo.CarWashRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-06-04  11:20
 */
@Service
public class WashRecordServiceImpl implements WashRecordService {
    @Resource
    private WashRecordDao washRecordDao;
    @Resource
    private StantionDao stantionDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<List<CarWashRecordVo>> findByStoreId(Integer storeId) {
        LocalDate today = LocalDate.now(); // 获取今天的日期
        LocalDate oneWeekAgo = today.minus(7, ChronoUnit.DAYS); // 往前减去7天
        QueryWrapper<CarWashRecord> wrapper=new QueryWrapper<>();
        wrapper.between("create_date",oneWeekAgo,today);
        wrapper.eq("store_id",storeId);
        wrapper.eq("wash_status",1);
        List<CarWashRecord> carWashRecords = washRecordDao.selectList(wrapper);
        LocalDate date = LocalDate.now();
        List<CarWashRecordVo> carWashRecordVos=new ArrayList<>();
        for (int i = 7; i > 0; i--) {
            CarWashRecordVo carWashRecordVo = new CarWashRecordVo();
            LocalDate dateaa = date.minusDays(i); // 往前计算i天
            carWashRecordVo.setTimePeriod(dateaa);
            carWashRecordVos.add(carWashRecordVo);
        }
        for (CarWashRecordVo cv:carWashRecordVos) {
            int i=0;
            for (CarWashRecord c: carWashRecords) {
                if(c.getCreateDate().toInstant().toString().substring(0,10).equals(cv.getTimePeriod().toString())){
                    i++;
                }
            }
            cv.setRecordCount(i);
        }
        return new Result<>(200,"获取近一星期洗车数据成功",carWashRecordVos);
    }

    @Override
    public Result<Integer> insertWa(BindPlusVo bindPlusVo) {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("font:login:" + token);
        FrontUser frontUser = JSON.parseObject(s, FrontUser.class);


        CarWashRecord carWashRecord = new CarWashRecord();
        carWashRecord.setUserId(frontUser.getUserId());
        carWashRecord.setStoreId(bindPlusVo.getStoreId());
        carWashRecord.setCarNo(bindPlusVo.getCarNo());
        carWashRecord.setCarId(bindPlusVo.getCarId());
        carWashRecord.setStationId(bindPlusVo.getStationId());
        carWashRecord.setIsNew("1");
        carWashRecord.setStartTime(new Date());
        carWashRecord.setWashStatus("0");
        carWashRecord.setCreateDate(new Date());
        System.out.println(carWashRecord.getCreateDate());
        int insert = washRecordDao.insert(carWashRecord);
        stantionDao.updateIdle(bindPlusVo.getStationId());
        return new Result<>(200,"洗车成功",insert);
    }
}
